<template>
  <div class="viewcontainer" v-if="dataset">
    <el-dialog :title="detailTitle" :visible.sync="isShowDialog" width="500px">
      <hi-form
        ref="detailForm"
        v-if="isShowDialog"
        :model="editData"
        :dataSource="dataset"
        v-bind="form"
      ></hi-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
    <el-row class="view-header">
      <el-col :span="22">
        <hi-lquery
          :target="dataset"
          :columnsTotal="2"
          v-bind="queryPanel"
        ></hi-lquery>
      </el-col>
      <el-col :span="2" style="text-align: right">
        <el-button type="primary" @click="showAdd">添加</el-button>
      </el-col>
    </el-row>
    <el-row class="view-main">
      <el-table
        ref="dataTable"
        :data="dataset.data"
        height="100%"
        style="width: 100%"
        v-loading="dataset.loading"
      >
        <el-table-column type="index" width="50" />
        <el-table-column prop="FDESC" label="名称" width="160" />
        <el-table-column prop="FINTEGRAL" label="积分" width="120" />
        <el-table-column prop="FDISCOUNT" label="折扣" width="120" />
        <el-table-column prop="FDISCOUNTOBJ" label="折扣对象" width="160" />
        <el-table-column label="积分累计规则">
          <template slot-scope="scope">
            {{ formatData(scope.row) }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="FCONSUME" label="每消费" width="120" align="right">
          <template slot-scope="scope">
            {{ formatMoney(scope.row.FCONSUME) }}
          </template>
        </el-table-column>
        <el-table-column prop="FACCUMULATE" label="累计积分" width="160" /> -->
        <el-table-column prop="FCREATEDATE" label="创建时间" width="180" />
        <el-table-column prop="FUSERID" label="创建人" width="160" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showEdit(scope)">
              <i class="el-icon-edit"></i>
            </el-button>
            <el-button type="text" size="small" @click="remove(scope)">
              <i class="el-icon-delete"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="view-footer pagebar">
      <hi-pagination
        background
        :total="dataset.total"
        :target="dataset"
      ></hi-pagination>
    </el-row>
  </div>
</template>

<script>
import {
  HiPagination,
  HiLquery,
  HiVquery,
  DataSetContainer,
  HiForm,
  utils,
} from "hi-ui";
import viewBase from "@/views/base";
import gradeDatacfg from "./dataset";
export default {
  name: "shoplist",
  extends: viewBase,
  components: { HiPagination, HiLquery, HiVquery, HiForm },
  data() {
    let datasetCfg = Object.assign({}, gradeDatacfg, {
      action: "autoAction",
    });
    return {
      //数据桥梁
      dataBridge: new Map(),
      detailTitle: null,
      //编辑状态
      editstate: "add", //add edit
      editData: {},
      isShowDialog: false,
      dataset: null,
      //表单配置
      form: {
        columnsTotal: 1,
        items: [
          {
            name: "FDESC",
          },
          {
            name: "FINTEGRAL",
          },
          {
            name: "FDISCOUNTOBJ",
            element: "el-select",
            props: {
              placeholder: "请选择",
            },
            children: [
              {
                element: "el-option",
                props: { value: "零售价", label: "零售价" },
              },
              {
                element: "el-option",
                props: { value: "会员价", label: "会员价" },
              },
            ],
          },
          {
            name: "FDISCOUNT",
            props: {
              min: 0,
              max: 10,
              placeholder: "请输入0~10的数字，9折输入9，95折输入9.5，以此类推",
            },
          },
          {
            name: "FCONSUME",
            props: {
              format: "￥0.00",
              precision: 0,
            },
          },
          {
            name: "FACCUMULATE",
          },
        ],
      },
      //查询面板配置
      queryPanel: {
        fields: [
          {
            element: "el-input",
            label: "名称",
            name: "FDESC",
          },
        ],
      },
      //数据集配置
      datasetCfg: datasetCfg,
      total: 100,
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    formatData(data) {
      return `每消费${data.FCONSUME}元增加${data.FACCUMULATE}个积分`;
    },
    formatMoney(val) {
      return utils.number.format(val, "￥");
    },
    showAdd() {
      this.isShowDialog = true;
      this.editstate = "add";
      this.detailTitle = "新增会员等级";
      this.editData = this.dataset.createEmptyData();
    },
    showEdit(data) {
      this.dataset.curRecord = data.row;
      this.editstate = "edit";
      this.detailTitle = "编辑会员等级-" + this.dataset.curRecord.FDESC || "";
      this.isShowDialog = true;
      this.editData = Object.assign({}, this.dataset.curRecord);
    },
    remove(data) {
      this.$confirm("确定要删除【" + data.row.FDESC + "】", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let saveHelper = this.createSaveHelper(this.dataset.modelFile);
          saveHelper.remove(data.row);
          saveHelper.save().then((res) => {
            this.dataset.reload();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {});
    },
    save() {
      let _result;
      this.$refs.detailForm.validate((result) => {
        _result = result;
      });
      if (!_result) return;
      if (this.editstate == "edit") {
        let curModel = this.dataset.getCurModel();
        curModel.set(this.editData);
      } else {
        this.dataset.add(this.editData);
      }

      if (this.dataset.getDirty()) {
        let promise = this.dataset.save();
        promise.then((res) => {
          this.isShowDialog = false;
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.dataset.clearUp();
        });
      } else {
        this.isShowDialog = false;
      }
    },
    afterquery() {
      this.dataset.load();
    },
  },
  mounted() {
    this.dataset = this.createDataSet(this.datasetCfg);
    // this.dataset = this.$dsContainer.createDataSet(this.datasetCfg);
    this.dataBridge.set(this.dataset, ["dataTable", "detailForm"]);
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
</style>

